$.fn.serializeObject   A
last analyzed

Complexity

Conditions 1
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 4
nop 0
dl 0
loc 15
rs 9.85
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 10 3
1
$(document).on("click", ".save-bet-button-dashboard", function (event) {
2
    event.preventDefault();
3
    let $_target = $(event.target);
4
    let $_bettingWrapperSelector = $('.betting-wrapper-dashboard');
5
    let $_bettingFormSelector = $('.user-betting-form-dashboard');
6
    let $_submitSelector = $('.save-bet-button-dashboard');
7
    let $_firstGameHiddenSelector = $('.firstGame-hidden-input');
8
    let $_secondGameHiddenSelector = $('.secondGame-hidden-input');
9
    let $_firstGameFakeSelector = $('.firstGame-fake-input');
10
    let $_secondGameFakeSelector = $('.secondGame-fake-input');
11
    let $_closestFirstFakeSelector = $_target.closest($_bettingWrapperSelector).find($_firstGameFakeSelector);
12
    let $_closestSecondFakeSelector = $_target.closest($_bettingWrapperSelector).find($_secondGameFakeSelector);
13
    let submitAction = '/savebet';
14
15
    $_target.closest($_bettingWrapperSelector).find($_firstGameHiddenSelector).val($_closestFirstFakeSelector.val());
16
    $_target.closest($_bettingWrapperSelector).find($_secondGameHiddenSelector).val($_closestSecondFakeSelector.val());
17
18
    $_submitSelector.prop('disabled', true);
19
    let data = $_target.closest($_bettingWrapperSelector).find($_bettingFormSelector).serializeObject();
20
21
    submitForm(data, $_submitSelector, submitAction);
22
23
});
24
25
$(document).on("click", ".save-bet-button", function (event) {
26
    event.preventDefault();
27
    let $_target = $(event.target);
28
    let $_bettingWrapperSelector = $('.betting-wrapper');
29
    let $_bettingFormSelector = $('.user-betting-form');
30
    let $_submitSelector = $('.save-bet-button');
31
    let submitAction = '/savebet';
32
33
    $_submitSelector.prop('disabled', true);
34
    let data = $_target.closest($_bettingWrapperSelector).find($_bettingFormSelector).serializeObject();
35
36
    submitForm(data, $_submitSelector, submitAction);
37
38
});
39
40
41
$(document).on("click", ".extra-bet-submit", function (event) {
42
    event.preventDefault();
43
    let $_target = $(event.target);
44
    let $_bettingWrapperSelector = $('.extra-bet-wrapper');
45
    let $_bettingFormSelector = $('.extra-bet-form');
46
    let $_submitSelector = $('.extra-bet-submit');
47
    let submitAction = '/saveextrabet';
48
49
    $_submitSelector.prop('disabled', true);
50
51
    let data = $_target.closest($_bettingWrapperSelector).find($_bettingFormSelector).serializeObject();
52
53
    submitForm(data, $_submitSelector, submitAction);
54
55
});
56
57
showNotification = function (message, color, icon) {
0 ignored issues
show
Bug introduced by
The variable showNotification seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.showNotification.
Loading history...
58
    $.notify({
59
        icon: "nc-icon " + icon,
60
        message: message
61
62
    }, {
63
        type: color,
64
        delay: 100,
65
        placement: {
66
            from: 'top',
67
            align: 'center'
68
        }
69
    });
70
};
71
72
$.fn.serializeObject = function () {
73
    let o = {};
74
    let a = this.serializeArray();
75
    $.each(a, function () {
76
        if (o[this.name] !== undefined) {
77
            if (!o[this.name].push) {
78
                o[this.name] = [o[this.name]];
79
            }
80
            o[this.name].push(this.value || '');
81
        } else {
82
            o[this.name] = this.value || '';
83
        }
84
    });
85
    return o;
86
};
87
88
function submitForm(data, $_submitSelector, url) {
89
    $.ajax({
90
        url: url,
91
        type: 'POST',
92
        dataType: 'json',
93
        data: data,
94
        success: function (data) {
95
            if (data.status) {
96
                showNotification("Tipp erfolgreich gespeichert!", 'success', 'nc-check-2')
97
            } else {
98
                showNotification("Fehler! 🙁", 'danger', 'nc-simple-remove')
99
100
            }
101
        },
102
        complete: function () {
103
            $_submitSelector.prop('disabled', false);
104
        }
105
    });
106
}
107
108